package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.Result;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.domain.Member;
import com.itheima.domain.Order;
import com.itheima.domain.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/8 19:41
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private OrderDao orderDao;

    //预约操作;
    @Override
    public Result submitOrder(Map map) throws ParseException {
        //定义一个OrderId，用于将新增的t_order表中的ID返回
        Integer orderId = null;
        //1.判断提交的数据中的人是否为会员
        //会员表  t_member
        String telephone = map.get("telephone").toString();
        Member member = memberDao.findByTelePhone(telephone);
        Integer memberId = null;//会员ID
        if (member != null) {
            //1.1 是会员
            memberId = member.getId();
        } else {
            //1.2 不是会员
            //member = getMember(map);
            member = new Member();
            member.setName(map.get("name").toString());
            member.setSex(map.get("sex").toString());
            member.setPhoneNumber(telephone);
            member.setIdCard(map.get("idCard").toString());
            member.setRegTime(new Date());
            member.setPassword(DigestUtils.md5DigestAsHex(telephone.substring(5).getBytes()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String idCard = map.get("idCard").toString().substring(6, 14);
            Date parse = sdf.parse(idCard);
            member.setBirthday(parse);
            memberDao.add(member);
            memberId = member.getId();
        }
        //2.判断预约的日期是否可以进行预约
        //将页面传递过来的日期字符串(yyyy-MM-dd)转换成日期类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = sdf.parse(map.get("orderDate").toString());
        //select * from t_ordersetting where orderDate = #{orderDate}
        OrderSetting ordersetting = orderSettingDao.getByOrderDate(orderDate);
        if (ordersetting != null) {
            //2.1可以
            //3.判断预约的日期是否已经约满
            //3.1已满
            //3.2未满
            Integer reservations = ordersetting.getReservations();//已预约人数
            Integer number = ordersetting.getNumber();//可预约人数
            if (reservations >= number) {
                //已经预满
                return new Result(false, "当天已经预约满");
            } else {
                //未约满
                //4.判断预约的信息是否重复 t_order[orderDate memberId setmealId]
                //4.1重复
                //4.2不重复
                Integer setmealId = Integer.parseInt(map.get("setmealId") + "");
                Order order = orderDao.findByCondition(orderDate, memberId, setmealId);
                if (order != null) {
                    return new Result(false, "已经预约过");
                } else {
                    /*//t_order表中添加一行数据
                    order = getOrder(memberId,orderDate,setmealId,map.get("orderType").toString());*/
                    order = new Order();
                    order.setMemberId(memberId);
                    order.setOrderDate(orderDate);
                    order.setOrderType(map.get("orderType").toString());
                    order.setOrderStatus("未到诊");
                    order.setSetmealId(setmealId);
                    //5.进行预约
                    orderDao.add(order); //预约成功
                    //6.将已预约人数+1
                    orderSettingDao.updateReservations(orderDate);
                    orderId = order.getId();
                }
            }
        } else {
            //2.2不可以
            return new Result(false, "不可以预约");
        }
        return new Result(true, "预约成功", orderId);
    }

    //通过ID查询用于预约成功后的数据回显;
    @Override
    public Map findById(Integer id) {

        return orderDao.findById(id);
    }
}
